#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/3/1 16:40
# @Author  : lirang
# @Site    :
# @File    : client.py
# @Software: PyCharm

import socket,json

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect(('10.78.1.100',8000))
sock.send('hello I am your user')

#获取文件头信息
recv_header = json.loads(sock.recv(512))
print "file_name:%s"%recv_header["file_name"]
print "file_type:%s"%recv_header["file_type"]
print "file_size:%s"%recv_header["file_size"]
print "date:%s"%recv_header["date"]
file_name = recv_header["file_name"]
file_size = recv_header["file_size"]
rest_size = file_size
fp = open(file_name, 'wb')
print 'recving...'
#循环读取数据
while 1:
    if rest_size > 10240:
        filedata = sock.recv(10240)
        fp.write(filedata)
        print "recv1:%d"%len(filedata)
    else:
        filedata = sock.recv(rest_size)
        print "recv2:%d"%len(filedata)
        fp.write(filedata)
        break
    if not filedata:
        print 'break'
        break
    rest_size = rest_size - len(filedata)
    if rest_size <= 0:
        print 'rest_size<0'
        break
fp.close()
print "rece done"
# recv_data = sock.recv(recv_header["file_size"])
# print "file content:\n\r%s"%recv_data

# with open('test.txt','w') as f:
#     f.write(recv_data)
sock.close()